import cv2
# 人脸识别，捕获人脸，生成人脸训练数据
facer = cv2.CascadeClassifier("./lbpcascades/lbpcascade_frontalface_improved.xml")
cap = cv2.VideoCapture(0)
cv2.resizeWindow('video', 640, 480)

index = 0
while cap.isOpened():
    ret, frame = cap.read()
    if ret:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = facer.detectMultiScale(gray, 1.1, 5)

        for (x, y, w, h) in faces:
            img_RIO = frame[y: y + h, x: x + w]
            fileName = "./data/" + str(index) + ".png"
            cv2.imwrite(fileName, img_RIO)
            index = index + 1
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
        cv2.imshow('video', frame)

    key = cv2.waitKey(10)
    if key & 0xFF == ord('q'):
        break
cv2.destroyAllWindows()
